﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Constructor.Frames;

namespace Constructor
{
    public class Production
    {
        private Slot _slot;
        public Slot OwnerSlot { get { return _slot; } }
        public Production(Slot slot)
        {
            _slot = slot;
            Marker = Properties.Settings.Default.defaultMarker;
            isProc = false;
            Procedure = new Procedure(this);
        }
        public NewItem Item { get; set; }
        public bool Marker { get; set; }
        public bool isProc { get; set; }
        public string Operation { get; set; }
        public Procedure Procedure { get; set; }
        public CommonLib.ValueType Type
        {
            get
            {
                if (Item != null && Item.Content != null)
                    switch (Item.Content.Type)
                    {
                        case CommonLib.SlotType.SubFrame:
                            return CommonLib.ValueType.Frame;
                        default:
                            if ((Item.Content as Slot).Domain != null)
                                switch ((Item.Content as Slot).Domain.Type)
                                {
                                    case CommonLib.DomainType.Image:
                                    case CommonLib.DomainType.Enum:
                                        return CommonLib.ValueType.Enum;
                                    case CommonLib.DomainType.Number:
                                        if (isProc) return CommonLib.ValueType.Int;
                                        int temp = 0;
                                        if (Value == null) return CommonLib.ValueType.None;
                                        if (int.TryParse(Value.Text, out temp))
                                            return CommonLib.ValueType.Int;
                                        else
                                            return CommonLib.ValueType.IntVar;
                                }
                            break;
                    }
                return CommonLib.ValueType.Frame;

            }
            set
            { }
        }
        public NewItem Value { get; set; }
        public void SaveInDataSet(DataSet ds)
        {
            DataTable dt = ds.Tables["Productions"];
            DataRow dr = dt.NewRow();
            dr["owner_slot"] = _slot.ID;
            dr["owner_frame"] = _slot.Frame.ID;
            if (Item != null)
                dr["slot"] = Item.Text;
            else
                dr["slot"] = "";
            dr["operation"] = Operation;
            dr["type"] = Type;
            if (Value != null)
                dr["value"] = Value.Text;
            else
                dr["value"] = "";
            dr["marker"] = Marker.ToString();
            dr["procedure"] = isProc.ToString();
            dt.Rows.Add(dr);
            Procedure.SaveInDataSet(ds);
        }
        public Production Clone(Slot s)
        {
            Production p = new Production(s);
            p.isProc = isProc;
            p.Marker = Marker;
            p.Operation = Operation;
            p.Type = Type;
            if (Item != null)
                p.Item = Item.Clone();
            if (Value != null)
                p.Value = Value.Clone();
            p.Procedure = Procedure.Clone(p);
            return p;
        }
    }
}
